<?php

namespace Admin\Controller;

use Think\Upload;



class PictureController extends BaseController {
    //图片显示
    public function lst(){
        
            
            $Picture_cat =D('picture_cat');
            $map['state']=1;
            $Pic = $Picture_cat->field('name,id,pid')->where($map)->select();
            $Pic1 = $Picture_cat->list_level($Pic);
            $this->assign('picture_cat',$Pic1);
            
            $Picture = D('Picture');
            $count = $Picture->count();
            $Page = new \Think\Page($count,20);
            $show = $Page->show();
            
            $type=I('type');
            if (I('search')==''){
                if ($type!=0){
                    $map['classify']=$type;
                }
                $Pic2 = $Picture->where($map)->order('createtime desc')->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('Carousel',$Pic2);
                $this->assign('page',$show);
            }else {
                if ($type!=0){
                    $map['classify']=$type;
                }
                $search =trim(I('search'));

                $map['name'] = array('like',"%$search%");

                $Pic2 = $Picture->where($map)->order('classify')->limit($Page->firstRow.','.$Page->listRows)->select();
                $this->assign('Carousel',$Pic2);
                $this->assign('page',$show);
            }
            $this->display();
        
    }
    
    
    //图片添加
    public function ad(){
       
            
            $Picture_cat =D('picture_cat');
            $map['state']=1;
            $Pic = $Picture_cat->where($map)->select();
            $Pic1 = $Picture_cat->list_level($Pic);
            $this->assign('picture_cat',$Pic1);
            
            $Picture = D('Picture');
            if (IS_POST){
                $data = array(
                    'name' => I('name'), 
                    'tags' => I('tags'),
                    'state' => I('state'),
                    'classify' => I('classify'),
                    'createtime' => time(),
                );
 
                 
                 $id = M('Picture')->max('id');
                 $pic_id=$id+1;
                 $upload =new Upload();
            if ($_FILES['url']['tmp_name']!=""){
                    $id = M('Picture')->max('id');
                    $pic_id=$id+1;
                    $upload =new Upload();
                    $upload->maxSize = 3145728;
                    $upload->exts =array('jpg', 'gif', 'png', 'jpeg');
                    $upload->rootPath  =  C('UPLOAD_PATH');
                    $upload->savePath = 'picture/pic/';
                    $upload->saveName =  'pic_'.intval($pic_id);
                    $upload->autoSub =  false;
                    
                    $info =$upload->upload();
                    if (!$info){
                        $this->error($upload->getError());
                    }else {
                        $data['url']=C('UPLOAD_PATH').$info['url']['savepath'].$info['url']['savename'];
                    }
                }
                if ($Picture->create($data)){
                    if ($Picture->add()){
                        $this->success('新增成功',U('lst'));
                    }else {
                        $this->error('新增失败！');
                    }
                }else {
                    $this->error($Picture->getError());
                }
                return ;
            }   
            $this->display();
        
    }

    
    
    public function edt($id){
        
            
            $Picture_cat =D('picture_cat');
            $map['state']=1;
            $Pic1 = $Picture_cat->field('name,id')->where($map)->select();
            $this->assign('picture_cat',$Pic1);
            
            $Picture = D('Picture');
            $Pic=$Picture->find($id);
            $this->assign('Carousel',$Pic);
            if (IS_POST){
                $data =array(
                    'id'=>I('id'),
                    'name'=>I('name'),
                    'tags'=>I('tags'),
                    'state'=>I('state'),
                    'classify'=>I('classify'),
                    'createtime'=>time(),
                );
              
                $upload =new Upload();
                if ($_FILES['url']['tmp_name']!=""){
                    unlink($Pic['url']);
                    $upload->maxSize = 3145728;
                    $upload->exts =array('jpg', 'gif', 'png', 'jpeg');
                    $upload->rootPath  =  C('UPLOAD_PATH');
                    $upload->savePath = 'picture/pic/';
                    $upload->saveName =  'pic_'.intval(I('id'));
                    $upload->autoSub =  false;
                    $info =$upload->upload();
                    if (!$info){
                        $this->error($upload->getError());
                    }else {
                        $data['url']=C('UPLOAD_PATH').$info['url']['savepath'].$info['url']['savename'];
                    }
                }
               
                if ($data['url']==""){
                    $data['url']=$Pic['url'];
                }
                if ($Picture->create($data)){
                    if ($Picture->save()){
                        $this->success('修改成功',U('lst'));
                    }else {
                        $this->error('修改失败！');
                    }
                }else {
                    $this->error($Picture->getError());
                }
    
                return ;
            }
    
    
            $this->display();
        
    
    
    }
    
    
    
    //图片单删除
    public function del($id){
        
            $Picture =D('Picture');
            if ($Picture->delete($id)){
                $pic=C('UPLOAD_PATH').'picture/pic/pic_'.intval($id);
                $code=C('UPLOAD_PATH').'picture/qr/qr_'.intval($id);
                unlink($pic);
                unlink($code);
                $this->success('删除成功！',U('lst'));
            }else {
                $this->error('删除失败');
            }
        

    }
    //全部删除
    public function del_both(){
        
            $del_both = I('del_both');
            if ($del_both!==""){
                $del_id =implode(',', $del_both);
                $Picture =D('Picture');
                if ($Picture->delete($del_id)){
                    $this->success('删除成功！',U('lst'));
                }else {
                    $this->error('删除失败');
                }
            }
        
        
    }

   
    public function recomme_index(){
        $Picture =D('Picture');
        $id=I('id');
        $is_index=I('is_index');
        
        $data=array(
                'id'=>$id,
                'is_index'=>$is_index,
        );
        if ($Picture->create($data)){
                if ($Picture->save()){
                    echo 1;
                }else {
                    echo 2;
                }
            }else {
                echo 2;
            }  
    }
  
    
    public function recomme_case(){
        $Picture =D('Picture');
        $id=I('id');
        $is_case=I('is_case');
    
        $data=array(
            'id'=>$id,
            'is_case'=>$is_case,
        );
        if ($Picture->create($data)){
            if ($Picture->save()){
                echo 1;
            }else {
                echo 2;
            }
        }else {
            echo 2;
        }
    }
    
    
    
    
    public function change_sort(){
        $Picture =D('Picture');
        $id=I('id');
        $sort =I('sort');
            $data=array(
                'id'=>$id,
                'sort'=>$sort,
            );
            $info =$Picture->save($data);
            if ($info){
                $res=array('info'=>'修改成功','status'=>'1');
            }
    }
    
    
    
}